name: Integration Tests

on:
  pull_request:
    branches: [ main ]

permissions:
  packages: read

jobs:
  Sqlite:
    runs-on: ubuntu-latest
    container:
      image: ghcr.io/kanboard/tests:latest
      credentials:
        username: ${{ github.repository_owner }}
        password: ${{ secrets.GITHUB_TOKEN }}
    steps:
    - uses: actions/checkout@v4
    - name: Validate composer.json and composer.lock
      run: composer validate
    - name: Install dependencies
      run: composer install --prefer-dist --no-progress --no-suggest
    - name: Start Apache
      run: /etc/init.d/apache2 start
    - name: Link document root
      run: |
        rm -rf /var/www/html
        ln -s $GITHUB_WORKSPACE /var/www/html
        cp tests/configs/config.sqlite.php /var/www/html/config.php
        chown -R www-data:www-data /var/www/html/data
        ls -l /var/www/html/
    - name: Integration tests with Sqlite
      run: ./vendor/bin/phpunit -c tests/integration.sqlite.xml

  Sqlite-PHP8:
    runs-on: ubuntu-22.04
    steps:
    - uses: actions/checkout@v4
    - name: Validate composer.json and composer.lock
      run: composer validate
    - name: Install dependencies
      run: composer install --prefer-dist --no-progress --no-suggest
    - name: Refresh APT index
      run: sudo apt update
    - name: Install Apache PHP module
      run: sudo apt install -y libapache2-mod-php
    - name: Start Apache
      run: sudo systemctl start apache2
    - name: Setup document root
      run: |
        sudo rm -rf /var/www/html
        sudo cp -R $GITHUB_WORKSPACE /var/www/html
        sudo cp tests/configs/config.sqlite.php /var/www/html/config.php
        sudo chown -R www-data:www-data /var/www/html/data
        ls -l /var/www/html/
    - name: Integration tests with Sqlite
      run: ./vendor/bin/phpunit -c tests/integration.sqlite.xml

  Postgres:
    runs-on: ubuntu-latest
    container:
      image: ghcr.io/kanboard/tests:latest
      credentials:
        username: ${{ github.repository_owner }}
        password: ${{ secrets.GITHUB_TOKEN }}
    services:
      postgres:
        image: postgres:9.4
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: kanboard
        ports:
        - 5432:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
    steps:
    - uses: actions/checkout@v4
    - name: Validate composer.json and composer.lock
      run: composer validate
    - name: Install dependencies
      run: composer install --prefer-dist --no-progress --no-suggest
    - name: Start Apache
      run: /etc/init.d/apache2 start
    - name: Link document root
      run: |
        rm -rf /var/www/html
        ln -s $GITHUB_WORKSPACE /var/www/html
        cp tests/configs/config.postgres.php /var/www/html/config.php
        chown -R www-data:www-data /var/www/html/data
        ls -l /var/www/html/
    - name: Integration tests with Postgres
      run: ./vendor/bin/phpunit -c tests/integration.postgres.xml
      env:
        DB_HOSTNAME: postgres
        DB_PORT: ${{ job.services.postgres.ports[5432] }}

  Mysql:
    runs-on: ubuntu-latest
    container:
      image: ghcr.io/kanboard/tests:latest
      credentials:
        username: ${{ github.repository_owner }}
        password: ${{ secrets.GITHUB_TOKEN }}
    services:
      mysql:
        image: mysql:5.7
        env:
          MYSQL_ROOT_PASSWORD: "kanboard"
          MYSQL_DATABASE: "kanboard"
          MYSQL_USER: "kanboard"
          MYSQL_PASSWORD: "kanboard"
        ports:
        - 3306:3306
        options: >-
          --health-cmd="mysqladmin ping"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 10
    steps:
    - uses: actions/checkout@v4
    - name: Validate composer.json and composer.lock
      run: composer validate
    - name: Install dependencies
      run: composer install --prefer-dist --no-progress --no-suggest
    - name: Start Apache
      run: /etc/init.d/apache2 start
    - name: Link document root
      run: |
        rm -rf /var/www/html
        ln -s $GITHUB_WORKSPACE /var/www/html
        cp tests/configs/config.mysql.php /var/www/html/config.php
        chown -R www-data:www-data /var/www/html/data
        ls -l /var/www/html/
    - name: Integration tests with Mysql
      run: ./vendor/bin/phpunit -c tests/integration.mysql.xml
      env:
        DB_HOSTNAME: mysql
        DB_PORT: ${{ job.services.mysql.ports[3306] }}
